package com.lzx.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lzx.model.FileEntity;
import com.lzx.model.cqjc.Cqjc;
import com.lzx.service.FileEntityService;
import com.lzx.service.cqjc.CqjcService;
import com.lzx.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/cqjc")
public class CqjcController {
    @Autowired
    private CqjcService cqjcService;
    @Autowired
    private FileEntityService fileEntityService;

    @RequestMapping("/cqjc.html")
    public ModelAndView cqjc(){
        ModelAndView mv = new ModelAndView("system/cqjc");
        return mv;
    }
   @RequestMapping("/tubiao.html")
    public ModelAndView tubiao(HttpServletRequest request){
       String obtime = request.getParameter("obtime");
       String current = request.getParameter("current");
       if (StringUtils.isEmpty(current)) {
           current = "1";
       }
       Cqjc cqjc = new Cqjc();
       cqjc.setoBservationTime(obtime);
       cqjc.setFrom(String.valueOf((Integer.parseInt(current) - 1) * 9));
       List<Cqjc> list = cqjcService.findByOnPage(cqjc);
       List<Cqjc> list1 = cqjcService.findList();
       ModelAndView mv = new ModelAndView("system/cqjc_liebiao");
       int size = list1.size();
       /*分页的页码数*/
       if (size % 9 == 0) {
           mv.addObject("titleNum", size / 9);
       } else {
           mv.addObject("titleNum", size / 9 + 1);
       }
       mv.addObject("listall",list);
       mv.addObject("current",current);
       mv.addObject("obtime",obtime);
        return mv;
    }
    @RequestMapping("/chakan.html")
    public ModelAndView chakan(String ywId) throws Exception {
        FileEntity fileEntity = new FileEntity();
        fileEntity.setYwId(ywId);
        List<FileEntity> list=  fileEntityService.findByYwId(ywId);
        String url="";
        for (int i = 0; i <list.size() ; i++) {
             url = list.get(i).getUrlPath();
        }
        ModelAndView mv = new ModelAndView("system/chakan");
        mv.addObject("url",url);
        return mv;
    }
    @RequestMapping("/tubiaoT.html")
    public String tubiaoT(){
       List<Cqjc> list = new ArrayList<>();
        List<Cqjc> listGroup = cqjcService.findListGroup();
        for (int i = 0; i <listGroup.size() ; i++) {
            String insect = listGroup.get(i).getInsect();
            List<Cqjc> listSum = cqjcService.findSum(insect);
            Cqjc cqjc = new Cqjc();
            cqjc.setInsect(insect);
            cqjc.setInsectNumber(listSum.get(0).getInsectNumber());
            list.add(cqjc);
        }
        return JSON.toJSONString(list);
    }
    @RequestMapping("/tubiaoB.html")
    public String tubiaoB(){
        JSONObject json = new JSONObject();
        List<Cqjc> list = cqjcService.findList();
        for (int i = 0; i <list.size() ; i++) {
            String date = list.get(i).getoBservationTime();
            date=date.substring(8,10);
            list.get(i).setoBservationTime(date);
        }
        List<Cqjc> listGroup = cqjcService.findListGroup();
        String all = JSON.toJSONString(list);
        String name = JSON.toJSONString(listGroup);
        json.put("name",name);
        json.put("all",all);
        return json.toJSONString();
    }
}
